package com.jmx.yp.servlet;

import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 功能描述：
 *         生成界面登录时的验证码
 * @author Jenny
 *
 * Date:2011-1-9 下午09:47:23
 */

public class VerifyCodeServlet extends HttpServlet {
	private final int CODE_LENGTH = 4;
	
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		ServletOutputStream outputstream = null;
		
		try {
			response.setHeader("Cache-Control", "no-store");
			outputstream = response.getOutputStream();
			String verifyCode = VerifyCodeEncoder.getVerifyCode();
			request.getSession().setAttribute("securityCode", verifyCode);

			// 将校验码写入输出流中
			VerifyCodeEncoder.encode(outputstream, verifyCode);

			outputstream.close();
			outputstream = null;

		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			if (outputstream != null)
				try {
					outputstream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
}
